package jsyf.oa.dao.wreport;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jsyf.oa.entity.wreport.WReportHostAndUnfinishEntity;
import jsyf.oa.entity.wreport.WReportUnfinishEntity;
import jsyf.oa.idao.BaseDao;
import jsyf.oa.ientity.IBaseEntity;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: @张巨波 @zhangjubo
 * @creat: on 2017/11/23.
 * @since: V1.0.0
 * @see jsyf.oa.web
 */
@Repository
public class WReportUnfinishDao extends BaseDao {
    public WReportUnfinishDao() {
  super.iBaseEntity=new WReportUnfinishEntity();
    }

    @Override
    public IBaseEntity convertJson2Entity(JSONObject json) {
        return  JSON.toJavaObject(json,WReportUnfinishEntity.class);
    }

    public List<WReportUnfinishEntity> getFinishEntityListByCondition(String condition, Object[] params) {
        List<WReportUnfinishEntity> entityList = new ArrayList<>();
        List<IBaseEntity> iBaseEntityList = super.getIBaseEntityByCondition(condition, params);
        for (int i = 0; i < iBaseEntityList.size(); i++) {
            entityList.add((WReportUnfinishEntity) iBaseEntityList.get(i));
        }
        return entityList;
    }
    public List<JSONObject> getUnfinishViewListByPage(String condition,Object[] params){
        List<JSONObject> resultList = new ArrayList<>();
        resultList = getViewJSONListByPage(condition,params,1,30);
        return resultList;
    }

    public List<JSONObject> getTrackList(String mainId) {
        StringBuffer sbSql = new StringBuffer();
        sbSql.append("select B.NAME USER_NAME,IFNULL(REPLAY_COUNT,0) REPLAY_COUNT,A.* from T_WRT_UNFINISH A  ");
        sbSql.append("LEFT JOIN T_SYS_USER B ON A.USER_ID=B.ID ");
        sbSql.append("LEFT JOIN (");
        sbSql.append("  SELECT TRACK_ID,COUNT(1) REPLAY_COUNT FROM T_WRT_UNFINISH_REPLY GROUP BY TRACK_ID");
        sbSql.append(") C ON A.ID=C.TRACK_ID ");
        sbSql.append("WHERE MAIN_ID in (" + mainId + ") ORDER BY CREATE_TIME DESC ");
        return queryForJsonList(sbSql.toString());
    }
}
